package com.coszero.utils.base

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.DefaultSpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.coszero.utils.callback.OnItemClickListener

/**
 * @param <T>
 * @author xmqian
 * ClassName: BaseRecyclerAdapter2
 *
 *
 * Author:oubowu
 *
 *
 * Fuction: RecyclerView通用适配器
 *
 *
 * 使用空布局时，需要传入空布局,getEmptyLayoutId()方法返回0为不使用空布局
 * CreateDate:2016/2/16 22:47
 *
 *
 * UpdateUser:
 *
 *
 * UpdateDate:
 *
 *
 * @version 1.0.1
 * @version 1
</T> */
abstract class BaseRecyclerAdapter2<T> @JvmOverloads constructor(
    context: Context, data: MutableList<T>?,
    useAnimation: Boolean = true,
    layoutManager: RecyclerView.LayoutManager? = null, status: Int = 0
) :
    BaseRecyclerAdapter<BaseRecyclerViewHolder?>(context, data) {
    protected var mData: List<T>
    protected var mUseAnimation: Boolean
    protected var mInflater: LayoutInflater
    protected var mClickListener: OnItemClickListener? = null
    protected var mShowFooter: Boolean = false
    protected var mStatus: Int = 0

    private val mLayoutManager: RecyclerView.LayoutManager?

    private val mLastPosition = -1

    constructor(context: Context, data: MutableList<T>?, status: Int) : this(
        context,
        data,
        true,
        null,
        status
    )

    init {
        mContext = context
        mUseAnimation = useAnimation
        mLayoutManager = layoutManager
        mData = data ?: ArrayList()
        mInflater = LayoutInflater.from(context)
        mStatus = status
    }

    protected val emptyLayoutId: Int
        /**
         * @return 空布局, 可选方法，如果不需要可以不实现
         */
        get() = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseRecyclerViewHolder {
        if (viewType == TYPE_FOOTER) {
            return BaseRecyclerViewHolder(
                mContext,
                mInflater.inflate(footerLayoutId, parent, false)
            )
        } else if (viewType == TYPE_EMPTY) {
            return BaseRecyclerViewHolder(mContext, mInflater.inflate(emptyLayoutId, parent, false))
        } else {
            val holder = BaseRecyclerViewHolder(
                mContext,
                mInflater.inflate(getItemLayoutId(viewType), parent, false)
            )
            if (mClickListener != null) {
                holder.itemView.setOnClickListener { v ->
                    mClickListener!!.onItemClick(
                        v,
                        holder.layoutPosition,
                        holder
                    )
                }
            }
            return holder
        }
    }

    protected val footerLayoutId: Int
        /**
         * @return 设置底部布局
         */
        get() = 0

    override fun onBindViewHolder(holder: BaseRecyclerViewHolder, position: Int) {
        if (getItemViewType(position) == TYPE_FOOTER) {
            if (mLayoutManager != null) {
                if (mLayoutManager is StaggeredGridLayoutManager) {
                    if (mLayoutManager.spanCount != 1) {
                        val params = holder.itemView
                            .layoutParams as StaggeredGridLayoutManager.LayoutParams
                        params.isFullSpan = true
                    }
                } else if (mLayoutManager is GridLayoutManager) {
                    if (mLayoutManager
                            .spanCount != 1 && mLayoutManager
                            .spanSizeLookup is DefaultSpanSizeLookup
                    ) {
                        throw RuntimeException("网格布局列数大于1时应该继承SpanSizeLookup时处理底部加载时布局占满一行。")
                    }
                }
            }
        } else if (getItemViewType(position) == TYPE_EMPTY) {
            setEmptyView(holder)
            //            bindData(holder, position, null);
//            showEmpty();
        } else {
            bindData(holder, position, mData[position])
            //            if (mUseAnimation) {
//                setAnimation(holder.itemView, position);
//            }
        }
    }

    /**
     * @param holder 可以对空布局的View进行操作
     */
    protected fun setEmptyView(holder: BaseRecyclerViewHolder?) {
    }

    protected fun setAnimation(viewToAnimate: View?, position: Int) {
//        if (position > mLastPosition) {
//            Animation animation = AnimationUtils
//                    .loadAnimation(viewToAnimate.getContext(), R.anim.item_bottom_in);
//            viewToAnimate.startAnimation(animation);
//            mLastPosition = position;
//        }
    }

    override fun onViewDetachedFromWindow(holder: BaseRecyclerViewHolder) {
        super.onViewDetachedFromWindow(holder)
        //        if (mUseAnimation && holder.itemView.getAnimation() != null && holder.itemView
//                .getAnimation().hasStarted()) {
//            holder.itemView.clearAnimation();
//        }
    }

    override fun getItemViewType(position: Int): Int {
        if (BaseRecyclerAdapter.Companion.isEmptyLs<T>(mData)) {
            return TYPE_EMPTY
        }
        if (mShowFooter && itemCount - 1 == position) {
            return TYPE_FOOTER
        }
        return TYPE_ITEM
    }

    override fun getItemCount(): Int {
        //是否需要显示底部
        val i = if ((footerLayoutId != 0 && mShowFooter)) 1 else 0
        //判断数据是否为空,再判断是否设置了空布局
        return if (!BaseRecyclerAdapter.Companion.isEmptyLs<T>(mData)) mData.size + i else (if (emptyLayoutId == 0) 0 else 1)
    }

    fun setOnItemClickListener(listener: OnItemClickListener?) {
        mClickListener = listener
    }

    abstract fun getItemLayoutId(viewType: Int): Int

    abstract fun bindData(holder: BaseRecyclerViewHolder?, position: Int, item: T)

    fun showFooter() {
        // KLog.e("Adapter显示尾部: " + getItemCount());
        notifyItemInserted(itemCount)
        mShowFooter = true
    }

    fun hideFooter() {
        // KLog.e("Adapter隐藏尾部:" + (getItemCount() - 1));
        notifyItemRemoved(itemCount - 1)
        mShowFooter = false
    }

    companion object {
        const val TYPE_ITEM: Int = 0
        const val TYPE_FOOTER: Int = 1
        const val TYPE_EMPTY: Int = 2
    }
}
